package cn.com.zero.manager.auth.service.impl;

import cn.com.zero.base.exception.ErrorCodeException;
import cn.com.zero.base.service.BaseService;
import cn.com.zero.base.vo.ResponseVO;
import cn.com.zero.manager.auth.api.constant.TokenKeyConstant;
import cn.com.zero.manager.auth.mapper.IPersonalMapper;
import cn.com.zero.manager.auth.po.ManagerPO;
import cn.com.zero.manager.auth.service.IPersonalService;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author guoxin
 * @version 1.0.0
 * @Description
 * @date 2023/3/3 9:33
 */
@Service
public class PersonalService extends BaseService implements IPersonalService {

    /**
     * 对称加密密钥
     */
    @Value(TokenKeyConstant.SECUREKEY)
    private String secureUtilKey;

    /**
     * 个人信息管理mapper
     */
    private IPersonalMapper personalMapper;

    /**
     * 修改个人信息
     *
     * @param userName  名称
     * @param mobile    手机号
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResponseVO updateInfo(String userName, String mobile) {
        String userid = getUserId();
        ManagerPO managerByNamePO = personalMapper.queryAdminByName(userName,userid);
        if(!Objects.isNull(managerByNamePO)){
            logger.error("用户名称{}已存在", userName);
            throw new ErrorCodeException(-1145140L);
        }
        String encrypt = SecureUtil.aes(secureUtilKey.getBytes()).encryptHex(mobile);
        ManagerPO managerByMobilePO = personalMapper.queryAdminByMobile(encrypt,userid);
        if (!Objects.isNull(managerByMobilePO)){
            logger.error("电话号码{}已存在", mobile);
            throw new ErrorCodeException(-114517L);
        }
        StringBuilder stringBuilder = new StringBuilder(mobile);
        StringBuilder showMobileBuilder = stringBuilder.replace(3,7,"****");
        String showMobile = showMobileBuilder.toString();
        personalMapper.updateInfo(userid,userName,encrypt,showMobile);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setMessage("修改成功");
        return responseVO;
    }

    /**
     * 解析token获取userId
     *
     * @return
     */
    private String getUserId(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String userid = request.getHeader(TokenKeyConstant.USERID);
        return userid;
    }


    /**
     * 修改密码
     *
     * @param oldPassword   原密码
     * @param newPassword   新密码
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResponseVO updatePassword(String oldPassword, String newPassword) {
        String userid = getUserId();
        ManagerPO managerPO = personalMapper.queryAdmin(userid);
        if(!SecureUtil.md5(oldPassword).equals(managerPO.getPassword())){
            logger.error("原密码错误");
            throw new ErrorCodeException(-1145150L);
        }

        String encode = SecureUtil.md5(newPassword);
        personalMapper.updatePassword(userid,encode);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setMessage("修改成功");
        return responseVO;
    }

    @Autowired
    public void setPersonalMapper(IPersonalMapper personalMapper) {
        this.personalMapper = personalMapper;
    }

}
